package com.castle.fortress.admin.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsSpecDto;
import com.castle.fortress.admin.goods.dto.GoodsSpecItemDto;
import com.castle.fortress.admin.goods.entity.GoodsSpecEntity;
import com.castle.fortress.admin.goods.entity.GoodsSpecItemEntity;
import com.castle.fortress.admin.goods.service.GoodsSpecItemService;
import com.castle.fortress.admin.goods.service.GoodsSpecService;
import com.castle.fortress.admin.utils.ExcelUtils;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品规格表 控制器
 *
 * @author
 * @since 2021-11-25
 */
@Api(tags = "商品规格表管理控制器")
@Controller
public class GoodsSpecController {
    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private GoodsSpecItemService goodsSpecItemService;

    /**
     * 商品规格表的分页展示
     *
     * @param goodsSpecDto 商品规格表实体类
     * @param current      当前页
     * @param size         每页记录数
     * @return
     */
    @ApiOperation("商品规格表分页展示")
    @GetMapping("/goods/goodsSpec/page")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:pageList")
    public RespBody<IPage<GoodsSpecDto>> pageGoodsSpec(GoodsSpecDto goodsSpecDto, @RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        Integer pageIndex = current == null ? GlobalConstants.DEFAULT_PAGE_INDEX : current;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<GoodsSpecDto> page = new Page(pageIndex, pageSize);
        IPage<GoodsSpecDto> pages = goodsSpecService.pageGoodsSpecExtends(page, goodsSpecDto);

        return RespBody.data(pages);
    }

    /**
     * 商品规格表的列表展示
     *
     * @param goodsSpecDto 商品规格表实体类
     * @return
     */
    @ApiOperation("商品规格表列表展示")
    @GetMapping("/goods/goodsSpec/list")
    @ResponseBody
    public RespBody<List<GoodsSpecDto>> listGoodsSpec(GoodsSpecDto goodsSpecDto) {
        List<GoodsSpecDto> list = goodsSpecService.listGoodsSpec(goodsSpecDto);
        list.forEach(item -> {
            final List<GoodsSpecItemEntity> itemList = goodsSpecItemService.list(new QueryWrapper<GoodsSpecItemEntity>().eq("spec_id", item.getId()));
            item.setSpecItemList(ConvertUtil.transformObjList(itemList, GoodsSpecItemDto.class));
        });
        return RespBody.data(list);
    }

    /**
     * 商品规格表保存
     *
     * @param goodsSpecDto 商品规格表实体类
     * @return
     */
    @ApiOperation("商品规格表保存")
    @PostMapping("/goods/goodsSpec/save")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:save")
    public RespBody<String> saveGoodsSpec(@RequestBody GoodsSpecDto goodsSpecDto) {
        if (goodsSpecDto == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsSpecEntity goodsSpecEntity = ConvertUtil.transformObj(goodsSpecDto, GoodsSpecEntity.class);
        if (goodsSpecService.save(goodsSpecEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格表编辑
     *
     * @param goodsSpecDto 商品规格表实体类
     * @return
     */
    @ApiOperation("商品规格表编辑")
    @PostMapping("/goods/goodsSpec/edit")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:edit")
    public RespBody<String> updateGoodsSpec(@RequestBody GoodsSpecDto goodsSpecDto) {
        if (goodsSpecDto == null || goodsSpecDto.getId() == null || goodsSpecDto.getId().equals(0L)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsSpecEntity goodsSpecEntity = ConvertUtil.transformObj(goodsSpecDto, GoodsSpecEntity.class);
        if (goodsSpecService.updateById(goodsSpecEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格表删除
     *
     * @param id
     * @return
     */
    @ApiOperation("商品规格表删除")
    @PostMapping("/goods/goodsSpec/delete")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:delete")
    public RespBody<String> deleteGoodsSpec(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (goodsSpecService.removeById(id)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 商品规格表批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("商品规格表批量删除")
    @PostMapping("/goods/goodsSpec/deleteBatch")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:deleteBatch")
    public RespBody<String> deleteGoodsSpecBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (goodsSpecService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品规格表详情
     *
     * @param id 商品规格表id
     * @return
     */
    @ApiOperation("商品规格表详情")
    @GetMapping("/goods/goodsSpec/info")
    @ResponseBody
    @RequiresPermissions("goods:goodsSpec:info")
    public RespBody<GoodsSpecDto> infoGoodsSpec(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        GoodsSpecDto goodsSpecDto = goodsSpecService.getByIdExtends(id);

        return RespBody.data(goodsSpecDto);
    }

    /**
     * 动态表头导出 依据展示的字段导出对应报表
     *
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @PostMapping("/goods/goodsSpec/exportDynamic")
    @ApiOperation("动态表头导出，依据展示的字段导出对应报表")
    public void exportDynamic(@RequestBody DynamicExcelEntity<GoodsSpecDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
        List<GoodsSpecDto> list = goodsSpecService.listGoodsSpec(dynamicExcelEntity.getDto());
        //字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
        List<List<Object>> dataList = null;
        /**
         * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
         */
        ExcelUtils.exportDynamic(response, dynamicExcelEntity.getFileName() + ".xlsx", null, list, dynamicExcelEntity.getHeaderList(), dataList);
    }


}
